package com.me.chat.controller.bis.relation;

import com.me.chat.pojo.relation.SingleRelationReq;
import com.me.chat.pojo.relation.qo.SingleRelationReqQO;
import com.me.chat.pojo.suport.ResultBean;
import com.me.chat.service.relation.SingleRelationReqService;
import com.me.chat.util.stauts.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * 用户关联请求
 * @Authhor: qiu
 * @Date: 2022/1/4 8:30 上午
 */

@RestController
@RequestMapping("/res/single_relation_req")
public class SingleRelationReqController {

    @Autowired
    SingleRelationReqService singleRelationReqService;

    /**
     * 查询出当前登录人下的别人发来的请求
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/search")
    public ResultBean<List<SingleRelationReq>> getSingleRelationByTargetId
            (@RequestParam(value = "current",required = false,defaultValue = "1") int current,
            @RequestParam(value = "size",required = false,defaultValue = "15") int size){
        List<SingleRelationReq> singleRelationReqByTargetId = singleRelationReqService.getSingleRelationReqByTargetId( current, size);
        return new ResultBean<>(StatusCode.SUCCEED,singleRelationReqByTargetId,"联系人查询获取成功");
    }

    /**
     * 根据接受者获取到好友请求到数量 支持分页
     * @param singleRelationReqQO（targetId）
     * @return
     */
    @GetMapping("/count_by_targets")
    public ResultBean<Integer> getSingleRelationCountByTargetId(SingleRelationReqQO singleRelationReqQO) {
        String targetId = singleRelationReqQO.getTargetId();
        //但凡符合一个条件
        boolean hasPass = targetId != null || targetId.length() > 0;
        //判断 发送者id与接受者id这参数是否存在
        if(hasPass) {
            int singleRelationReqs = singleRelationReqService.getSingleRelationReqCountByTargetId(singleRelationReqQO);
            return new ResultBean<>(StatusCode.SUCCEED,singleRelationReqs,"联系人请求数量获取成功");
        }else {
            //两者都不符合，可能会导致sql的权量查询，是个可能会搞崩数据库的危险操作
            return new ResultBean<>(StatusCode.ERROR,null,"参数不能为空");
        }
    }



    /**
     * 用户关联请求添加
     * @param singleRelationReqQO (targetId)
     * @return
     */
    @PostMapping("/add")
    public ResultBean<Integer> addSingleRelationReq(@RequestBody SingleRelationReqQO singleRelationReqQO) {
        try {
            int messageCode = singleRelationReqService.addSingleRelationRequest(singleRelationReqQO);
            return new ResultBean(StatusCode.SUCCEED,messageCode,"用户关联请求添加成功返回id");
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ResultBean(StatusCode.ERROR,null,"异常操作");
        }
    }



}
